import main
import logging
import Config

if Config.ENABLE_LOGGING:
    viewLogger = logging.getLogger('gtap.views')
    viewLogger.setLevel(Config.LOGGING_LEVEL)
    handler = logging.FileHandler(Config.LOGGING_PATH, 'a')
    formatter = logging.Formatter(Config.LOGGING_FORMAT)
    handler.setFormatter(formatter)
    viewLogger.addHandler(handler)

def mainPage(request):
    viewLogger.info('[' + request.method + '] ' + request.build_absolute_uri())
    mainpage = main.MainPage()
    response = mainpage.do_proxy(request)
    viewLogger.info(response.status_code)
    if response.status_code != 200:
        viewLogger.error(str(request))
        viewLogger.error(str(response))
    
    return response


def oauthPage(request):
    viewLogger.info('[' + request.method + '] ' + request.build_absolute_uri())
    path = str(request.path)
    mode = path.split('/')[2]
    
    oauthpage = main.OauthPage()
    
    if request.method == 'GET':
        response = oauthpage.get(request, mode)
        viewLogger.info(response.status_code)
        if response.status_code != 200:
            viewLogger.error(str(request))
            viewLogger.error(str(response))
        return response
    
    elif request.method == 'POST':        
        response = oauthpage.post(request, mode)
        viewLogger.info(response.status_code)
        if response.status_code != 200:
            viewLogger.error(str(request))
            viewLogger.error(str(response))
        return response
        
